//
//  CAVipSubView.m
//  0.3camera
//
//  Created by 茶 on 2023/11/27.
//

#import "CAVipSubView.h"

@interface CAVipSubView ()

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;

@property (nonatomic, strong) UIImageView *selectedImgView;
@property (nonatomic, strong) UIButton *cornerView;

@end

@implementation CAVipSubView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    _contentView = [UIView new];
    _contentView.layer.cornerRadius = CARatioWidth(15);
    _contentView.backgroundColor = UIColor.whiteColor;
    [self addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    _titleLabel = [UILabel new];
    _titleLabel.font = CAFontScaleRegular(18);
    _titleLabel.textColor = HexColor(@"#666666");
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.ca_offset(28);
    }];
    
    _priceLabel = [UILabel new];
    _priceLabel.font = CAFontScaleSemibold(15);
    _priceLabel.textColor = HexColor(@"#29282A");
    _priceLabel.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:_priceLabel];
    [_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.ca_offset(-28);
    }];
    
    _cornerView = [UIButton buttonWithType:UIButtonTypeCustom];
    _cornerView.layer.cornerRadius = CARatioWidth(16);
    _cornerView.clipsToBounds = YES;
    _cornerView.layer.borderColor = HexColor(@"#29282A").CGColor;
    _cornerView.layer.borderWidth = 1;
    [_cornerView addTarget:self action:@selector(buttonEvent) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:_cornerView];
    [_cornerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    _selectedImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Vip_Selected"]];
    [_cornerView addSubview:_selectedImgView];
    [_selectedImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
    }];
}

- (void)buttonEvent {
    CABlock(self.selectBlock, self.tag);
}

- (void)setTitle:(NSString *)title {
    _titleLabel.text = _title = title;
}

- (void)setPrice:(NSString *)price {
    _price = price;
    NSString* currencySymbol = [NSLocale.currentLocale objectForKey:NSLocaleCurrencySymbol];
    NSString *priceStr = [NSString stringWithFormat:@"%@ %@", currencySymbol, price];
    
    NSUInteger length = [currencySymbol length];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:priceStr];
    [attri addAttribute:NSFontAttributeName value:CAFontScaleSemibold(13) range:NSMakeRange(0, length)];
    
    _priceLabel.attributedText = attri;
}


- (void)setSelected:(BOOL)selected {
    _selected = selected;
    if (selected){
        _cornerView.layer.borderWidth = 1;
        _selectedImgView.hidden = NO;
    } else {
        _cornerView.layer.borderWidth = 0;
        _selectedImgView.hidden = YES;
    }
}
@end
